<?php
require_once('ami.php'); 
$config_dir="/etc/asterisk-mihira/";

$ami_conn=new ami();
$ami_conn->Login();


//Connect to config Files
$file_sip_additional = fopen($config_dir."sip_additional.conf","w+");
$file_extensions_additional = fopen($config_dir."extensions_additional.conf","w+");

//Connect to MySQL Database
$con=mysqli_connect("localhost","root","PASSWORD1","dbasterisk");

// Check MySQL connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//Writing to  extensions_additional.conf:  Class Of Services and Dial Permissions
fwrite($file_extensions_additional, "\n;Class of Services\n");
$result = mysqli_query($con,"SELECT c.`id`,c.`name`, d.`name` as 'permission', d.`dial_pattern` FROM `class_of_services` c join `cos_dp_map` m  on m.`cos`=c.`id` join `dial_permisions` d on m.`dial_permision` = d.`id` order by c.`id`");
$id=-6;
while($row = mysqli_fetch_array($result))
{
	if($id!=$row['id'])
	{
		$id=$row['id'];
		fwrite($file_extensions_additional, "\n[".$row['name']."]\n");
	}
	
	fwrite($file_extensions_additional, "exten => ".$row['dial_pattern'].',1,GoSub(subManipulations,${EXTEN},1)		;'.$row['permission']."\n");
	fwrite($file_extensions_additional, '        same => n,Goto(outbound-routing-table,${EXTEN},1)'."\n");
}


fwrite($file_extensions_additional, "\n[Inbound-Calls-From-Trunk-PBX-VGW]\n");
fwrite($file_extensions_additional, 'exten => _029000[0,1]XXX,1,GoSub(subManipulations,${EXTEN},1)'."\n");
fwrite($file_extensions_additional, '        same => n,Goto(inbound-routing-table,${EXTEN},1))'."\n");
	

//Writing to  extensions_additional.conf:  Parameter Manipulation
fwrite($file_extensions_additional, "\n; Manipulations\n");
fwrite($file_extensions_additional, "\n[subManipulations]\n");
$result = mysqli_query($con,"SELECT * FROM `parameter_manipulation` ");
$id=-6;
while($row = mysqli_fetch_array($result))
{
	fwrite($file_extensions_additional, "exten => ".$row['did_pattern']."/".$row['cid_pattern'].',1,'.$row['manipulation'].	"	;".$row['name']."\n");
	fwrite($file_extensions_additional, '        same => n,Return()'."\n");
}
fwrite($file_extensions_additional, "exten => _X!,1,NoOp()\n");
fwrite($file_extensions_additional, "        same => n,Return()\n");
	


//Writing to  extensions_additional.conf:  Routing Table 
fwrite($file_extensions_additional, "\n[outbound-routing-table]\n");
fwrite($file_extensions_additional, "include => local-extensions\n");
fwrite($file_extensions_additional, "include => outbound-routes\n");




//Writing to  extensions_additional.conf:  Outbound Routes 
fwrite($file_extensions_additional, "\n[outbound-routes]\n");
/*
$result = mysqli_query($con,"SELECT r.`name`, r.`destination_pattern`,r.`did`, t.`name` as 'trunk' FROM `outbound_routes` r join `trunks` t on r.`trunk_id`=t.`id` where r.`status`=1");
while($row = mysqli_fetch_array($result))
{
	fwrite($file_extensions_additional, "exten => ".$row['destination_pattern'].',1,Dial(Sip/'.$row['did'].'@'.$row['trunk'].")		;".$row['name']."\n");

}
*/

fwrite($file_extensions_additional, 'exten => _04XXXXXXXX,1,Dial(Sip/${EXTEN}@PBX-VGW)               ;Mobile Numbers'."\n");
fwrite($file_extensions_additional, 'exten => _***XXXXXXXXXX#XXXX,1,Dial(Sip/${EXTEN:14:${MATH(${LEN(${EXTEN})}-14,int)}})           ;Dialed_from_HS'."\n");
fwrite($file_extensions_additional, 'exten => _***XXXXXXXXXX#XXXXXXXXXX,1,Dial(Sip/${EXTEN:14:${MATH(${LEN(${EXTEN})}-14,int)}}@PBX-VGW)             ;Dialed_from_HS'."\n");



//Writing to  extensions_additional.conf:  Inbound Routes 
fwrite($file_extensions_additional, "\n[inbound-routing-table]\n");
$result = mysqli_query($con,"SELECT * FROM `inbound_routes` ");
while($row = mysqli_fetch_array($result))
{
	if ($row['dest_type']=='queue')
	{
		fwrite($file_extensions_additional, "exten => _".$row['did_pattern'].",1,Queue(".$row['destination'].")		;".$row['name']."\n");
	}
	else if ($row['dest_type']=='extension')
	{
		fwrite($file_extensions_additional, "exten => _".$row['did_pattern'].",1,Dial(Sip/".$row['destination'].")		;".$row['name']."\n");
	}
}





//Writing to sip_additional.conf : Extensions
//Writing to  extensions_additional.conf:  Routing Table > local-extensions
fwrite($file_extensions_additional, "\n[local-extensions]\n");
$result = mysqli_query($con,"SELECT * FROM `sip_accounts` where `status`=1");
while($row = mysqli_fetch_array($result))
{
	// Write to sip_additional
	fwrite($file_sip_additional, "\n[".$row['id'] . "]\n");
	fwrite($file_sip_additional, "secret=".$row['secret'] . "\n");
	fwrite($file_sip_additional, "dtmfmode=".$row['dtmfmode'] . "\n");
	fwrite($file_sip_additional, "canreinvite=".$row['canreinvite'] . "\n");
	fwrite($file_sip_additional, "context=".$row['cos'] . "\n");
	fwrite($file_sip_additional, "type=".$row['type'] . "\n");
	fwrite($file_sip_additional, "nat=".$row['nat'] . "\n");
	fwrite($file_sip_additional, "qualify=".$row['qualify'] . "\n");
	fwrite($file_sip_additional, "qualifyfreq=".$row['qualifyfreq'] . "\n");
	fwrite($file_sip_additional, "encryption=no\n");
	fwrite($file_sip_additional, "deny=".$row['deny'] . "\n");
	fwrite($file_sip_additional, "permit=".$row['permit'] . "\n");
	fwrite($file_sip_additional, "callerid=".$row['callerid'] . "\n");	
	fwrite($file_sip_additional, "host=".$row['host'] . "\n");
		$disallows = explode(";", $row['disallow']);
		foreach ($disallows as &$value) {
			fwrite($file_sip_additional, "disallow=".$value . "\n");
		}

		$allows = explode(";", $row['allow']);
			foreach ($allows as &$value) {
				fwrite($file_sip_additional, "allow=".$value . "\n");
			}
	fwrite($file_sip_additional, "\n\n");

	// Write to extensions_additional
	fwrite($file_extensions_additional, "exten => ".$row['id'].",1,Dial(Sip/".$row['id'].")		;".$row['name']."\n");
	//fwrite($file_extensions_additional, "\n");
}
  
  
   
	fwrite($file_extensions_additional, "\n[from-queue]\n".'exten => _X!,1,Goto(local-extensions,${QAGENT},1)'."\n"); 

	
// Writing SIP Peers (Trunk Ends) to sip_additional.conf)
$result = mysqli_query($con,"SELECT * FROM `trunks` where `status`=1");
while($row = mysqli_fetch_array($result))
  {
	fwrite($file_sip_additional, "[".$row['name'] . "]\n");
	//fwrite($file_sip_additional, "secret=".$row['secret'] . "\n");
	fwrite($file_sip_additional, "context=".$row['cos'] . "\n");
	fwrite($file_sip_additional, "type=".$row['type'] . "\n");
	fwrite($file_sip_additional, "host=".$row['host'] . "\n");
	fwrite($file_sip_additional, "\n\n");
  }

  

//Writing to  extensions_additional.conf:  Routes to QUEUEs
$result = mysqli_query($con,"SELECT * FROM `trunks` where `status`=1");
while($row = mysqli_fetch_array($result))
  {
	fwrite($file_sip_additional, "[".$row['name'] . "]\n");
	//fwrite($file_sip_additional, "secret=".$row['secret'] . "\n");
	fwrite($file_sip_additional, "context=".$row['cos'] . "\n");
	fwrite($file_sip_additional, "type=".$row['type'] . "\n");
	fwrite($file_sip_additional, "host=".$row['host'] . "\n");
	fwrite($file_sip_additional, "\n\n");
  }





  
echo $ami_conn->Reload();

  
mysqli_close($con);
fclose($file_sip_additional);
$ami_conn->Logout();
?> 

